﻿namespace EasyWeb
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    /// <summary>
    /// The base class for the other collection classes used by EasyWeb.
    /// </summary>
    /// <typeparam name="T">The type of elements in the collection.</typeparam>
    [Serializable]
    public abstract class EWCollection<T> : IList<T>, IReadOnlyList<T>
        where T : class
    {
        private readonly List<T> list = new List<T>();

        protected EWCollection()
        {
        }

        protected EWCollection(IEnumerable<T> collection)
        {
            EWGuard.IsNotNull(collection, "collection");

            this.list.AddRange(collection);
        }

        public int IndexOf(T item)
        {
            EWGuard.IsNotNull(item, "item");

            return this.list.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            EWGuard.IsNotNull(item, "item");

            this.list.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            this.list.RemoveAt(index);
        }

        public T this[int index]
        {
            get { return this.list[index]; }
            set
            {
                EWGuard.IsNotNull(value, "value");

                this.list[index] = value;
            }
        }

        public void Add(T item)
        {
            EWGuard.IsNotNull(item, "item");

            this.list.Add(item);
        }

        public void Clear()
        {
            this.list.Clear();
        }

        public bool Contains(T item)
        {
            EWGuard.IsNotNull(item, "item");

            return this.list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            this.list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.list.Count; }
        }

        public bool IsReadOnly
        {
            get { return ((IList<T>)this.list).IsReadOnly; }
        }

        public bool Remove(T item)
        {
            return this.list.Remove(item);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return this.list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)this.list).GetEnumerator();
        }
    }
}
